﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;


namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eSetting : EditBase
    {
        #region Public Members

        public bool IsNewItem
        {
            get { return (ActiveItemId == Guid.Empty); }
        }

        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            if (ActiveItemId != Guid.Empty)
            {
                bindItem();
            }
            else
            {
                bindLists(new SettingInfo());

            }

            ddCategory.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(ddCategory_SelectedIndexChanged);
        }


        #endregion

        #region Event Handlers

        protected void ddCategory_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindSubCategories(new Guid(ddCategory.SelectedValue), new Guid());
        }


        #endregion

        #region Helpers

        private void bindItem()
        {
            SettingController sc = new SettingController();
            SettingInfo si = sc.SelectSetting(ActiveItemId);
            if (si != null)
            {
                lblHeaderRight.Text = si.Name;
                txtName.Text = si.Name;
                txtIndex.Text = si.SettingIndex.ToString();
                chkEncrypted.Checked = si.Encrypted;
                bindLists(si);
            }


        }

        private void bindLists(SettingInfo si)
        {
            bindCategories(si.CategoryId);
            bindSubCategories(new Guid(ddCategory.SelectedValue), si.SubcategoryId);
        }


        private void bindCategories(Guid CategoryId)
        {
            CategoryController cc = new CategoryController();
            List<CategoryInfo> ciList = cc.SelectCategories("Name Asc");
            ddCategory.DataSource = ciList;
            ddCategory.DataTextField = "Name";
            ddCategory.DataValueField = "CategoryId";
            ddCategory.DataBind();
            if (CategoryId != Guid.Empty)
                if (ddCategory.Items.FindItemByValue(CategoryId.ToString()) != null)
                    ddCategory.Items.FindItemByValue(CategoryId.ToString()).Selected = true;
                else
                    ddCategory.SelectedIndex = 0;

        }

        private void bindSubCategories(Guid CategoryId, Guid SubCategoryId)
        {
            SubcategoryController sc = new SubcategoryController();
            List<SubcategoryInfo> scList = sc.SelectSubcategoriesByCategory(CategoryId, "Name Asc");
            ddSubCategory.DataSource = scList;
            ddSubCategory.DataTextField = "Name";
            ddSubCategory.DataValueField = "SubcategoryId";
            ddSubCategory.DataBind();
            if (SubCategoryId != Guid.Empty)
                if (ddSubCategory.Items.FindItemByValue(SubCategoryId.ToString()) != null)
                    ddSubCategory.Items.FindItemByValue(SubCategoryId.ToString()).Selected = true;
                else
                    ddSubCategory.SelectedIndex = 0;
        }

        #endregion

        #region Action Event Helpers

        private void SaveItem()
        {
            if (Page.IsValid)
            {
                SettingController sc = new SettingController();
                SettingInfo si = new SettingInfo();
                if (!IsNewItem)
                    si = sc.SelectSetting(ActiveItemId);
                si.Name = txtName.Text;
                si.SettingIndex = common.TryToParse(txtIndex.Text);
                si.CategoryId = new Guid(ddCategory.SelectedValue);
                si.SubcategoryId = new Guid(ddSubCategory.SelectedValue);
                si.Encrypted = chkEncrypted.Checked;

                if (IsNewItem)
                {
                    sc.InsertSetting(si);
                }
                else
                {
                    sc.UpdateSetting(si);
                }

                RaiseNavEvent(this, new NavEventArgs("eSettings", ActiveItemId));
            }


        }

        private void DeleteItem()
        {
            SettingController sc = new SettingController();
            sc.DeleteSetting(ActiveItemId);
            RaiseNavEvent(this, new NavEventArgs("eSettings", ActiveItemId));
        }

        #endregion

        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Save":
                    SaveItem();
                    break;
                case "Delete":
                    DeleteItem();
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eSettings", ActiveItemId));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Save", "Save Changes", "~/images/icons/save.png", "", true, true));
                al.Add(new ActionInfo("Delete", "Delete Setting", "~/images/icons/delete.png", "Deleting this setting will also remove it from any configurations. Are you sure you wish to delete this setting?", !IsNewItem));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));
                return al;
            }
        }

        #endregion
    }
}